package cn.edu360.day06;

import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.concurrent.Callable;

public class CallableClient implements Callable<String> {

	final static String KEY = "天王盖地虎";
	String path;
	String comd;
	String Ip;
	String localpath;

	public CallableClient() {
		super();
	}

	public CallableClient(String path, String comd, String ip, String localpath) {
		super();
		this.path = path;
		this.comd = comd;
		Ip = ip;
		this.localpath = localpath;
	}

	public String call() throws Exception {
		String result = "";
		try {
			Socket socket = new Socket(Ip, 8888);
			ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
			oos.writeObject(KEY);
			oos.flush();
			// 接受信息并返回给服务器端信息
			ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
			String value = (String) ois.readObject();
			if ("宝塔镇河妖".equals(value)) {
				oos.writeObject(path);
				oos.flush();
				// 发送jar文件
				FileInputStream fis = new FileInputStream(localpath);
				byte[] b = new byte[1024];
				int line = 0;
				while ((line = fis.read(b)) != -1) {
					oos.write(b, 0, line);
				}
				oos.flush();
				fis.close();
				// 发送comd
				oos.writeObject(comd);
				oos.flush();
				// 接受jar文件
				value = (String) ois.readObject();
			} else {
				System.out.println("身份验证失败.....");
			}
			socket.shutdownInput();
			socket.shutdownOutput();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

}
